package com.smile.f213;
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ListDemo {
	private JLabel label;
	public void createUI(){
		JFrame frame = new JFrame("列表框示例程序");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		DefaultListModel<String> listModel = new DefaultListModel<String>();
		listModel.addElement("选项一");
		listModel.addElement("选项二");
		listModel.addElement("选项三");
		
		JList<?> list = new JList<>(listModel);
		list.addListSelectionListener(new ListAction());
		
		label = new JLabel();
		frame.getContentPane().add(list,BorderLayout.NORTH);
		frame.getContentPane().add(label,BorderLayout.SOUTH);
		
		frame.setBounds(200, 200, 250, 120);
		frame.setVisible(true);
	}
	class ListAction implements ListSelectionListener{
		@Override
		public void valueChanged(ListSelectionEvent e) {
			JList<?> li = (JList)e.getSource();
			label.setText(li.getSelectedValue().toString()+"被选择了");
		}
	}
	public static void main(String[] args) {
		ListDemo example = new ListDemo();
		example.createUI();
	}
}